package de.dhbw.asa.controller;

import java.sql.ResultSet;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;

import de.dhbw.asa.model.Database;
import de.dhbw.asa.model.Product;

@ManagedBean
@SessionScoped
public class SearchProductsCtrl extends Database {

	String sSearchString;
	private ArrayList<Product> products;

	public void search()
	{
		System.out.println("Funktion Suchen");
		try {
			products = new ArrayList<Product>();
			this.createConnection();
			StringBuffer sbQuery = new StringBuffer();
			sbQuery.append("Select * from Product where title like '%");
			sbQuery.append(sSearchString);
			sbQuery.append("%';");
			System.out.println("Suchstring: "+sbQuery.toString());
			ResultSet jResult = this.executeQuery(sbQuery.toString());
			while (jResult.next()) {
				Product jProduct = new Product();
				jProduct.load(jResult.getInt("productNr"));
				products.add(jProduct);
			}
			this.closeConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String getsSearchString() {
		return sSearchString;
	}

	public void setsSearchString(String jsSearchString) {
		this.sSearchString = jsSearchString;
	}

	public ArrayList<Product> getProducts() {
		return products;
	}

	public void setProducts(ArrayList<Product> products) {
		this.products = products;
	}
	
	

}
